I am working on login with React JS and wordpress with the plugin JWT Authentication for WP REST API. I would like to get token but I have a problem
The code is here:
//...
function Login() {
const[username, setUsername] = useState("")
const[password, setPassword] = useState("")
const access = async () => {
const response = await fetch(`http://wp-playground.test/wp-json/jwt-auth/v1/token`, {
method: 'POST',
header: {
'Content-Type' : 'application/json',
},
body : JSON.stringify({
username: username,
password: password
})
})
const data = await response.json()
console.log(data)
setUsername("")
setPassword("")
}
return (
<div>
<h2>Login</h2>
<label>Username: </label><input value={username} onChange={(e) => setUsername(e.target.value)}/><br/><br/>
<label>Password: </label><input type="password" value={password} onChange={(e) => setPassword(e.target.value)}/><br/><br/>
<button onClick={access}>Login</button>
</div>
)
and I get like this:
{
code: "[jwt_auth] empty_username"
data: {
status: 403
}
message: "<strong>Error</strong>: The username field is empty."
}
Thanks for your attention!